package net.wanho.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.dto.ProductDTO;
import net.wanho.entity.Product;
import net.wanho.mapper.ProductMapper;
import net.wanho.service.ProductService;
import net.wanho.util.FileUtil;
import net.wanho.vo.PageVo;
import net.wanho.vo.ProductVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【product(商品表)】的数据库操作Service实现
* @createDate 2025-05-05 02:08:26
*/
@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService{
    private final ProductMapper productMapper;
    @Override
    public PageVo<ProductVo> productPageList(ProductDTO productDTO, Integer pageNum, Integer pageSize) {
        IPage<ProductVo> page = new Page<>(pageNum, pageSize);
        QueryWrapper<ProductVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(productDTO.getStoreId()),"st.store_id", productDTO.getStoreId())
                .like(ObjectUtil.isNotEmpty(productDTO.getProductName()), "p.product_name", productDTO.getProductName())
                .like(ObjectUtil.isNotEmpty(productDTO.getBrandName()), "b.brand_name", productDTO.getBrandName())
                .like(ObjectUtil.isNotEmpty(productDTO.getTypeName()), "pt.type_name", productDTO.getTypeName())
                .like(ObjectUtil.isNotEmpty(productDTO.getSupplyName()), "su.supply_name", productDTO.getSupplyName())
                .like(ObjectUtil.isNotEmpty(productDTO.getPlaceName()), "pl.place_name", productDTO.getPlaceName())
                .eq(ObjectUtil.isNotEmpty(productDTO.getUpDownState()), "p.up_down_state", productDTO.getUpDownState())
                .lt(Objects.nonNull(productDTO.getIsOverDate()) && productDTO.getIsOverDate() == 1, "p.supp_date", LocalDateTime.now())
                .gt(Objects.nonNull(productDTO.getIsOverDate()) && productDTO.getIsOverDate() == 0, "p.supp_date", LocalDateTime.now());
        List<ProductVo> productVoList = productMapper.selectProductList(page, queryWrapper);

        return new PageVo<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.offset(),
                productVoList
        );
    }

    @Override
    public void updateProductById(ProductVo productVo) {
        Product product = new Product();
        BeanUtils.copyProperties(productVo, product);
        int num = productMapper.updateById(product);
        if (num == 0) {
            throw new RuntimeException("商品修改失败！");
        }
    }

    @Override
    public void removeProductById(Integer id) {
        Product product = productMapper.selectById(id);
        if(ObjectUtil.isNotEmpty(product.getImgs())) {
            FileUtil.deleteFile(product.getImgs());
        }
        int num = productMapper.deleteById(id);
        if (num == 0) {
            throw new RuntimeException("商品删除失败！");
        }
    }

    @Override
    public void removeProductBatchByIds(List<Integer> idList) {
        List<Product> productList = productMapper.selectBatchIds(idList);
        List<String> imgsList = productList.stream().map(Product::getImgs).collect(Collectors.toList());
        FileUtil.deleteFileBatch(imgsList);

        int num = productMapper.deleteBatchIds(idList);
        if (num == 0) {
            throw new RuntimeException("商品批量删除失败！");
        }
    }
}




